package cn.iocoder.yudao.module.trade.controller.admin.depositproductrelation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.trade.controller.admin.depositproductrelation.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.depositproductrelation.DepositProductRelationDO;
import cn.iocoder.yudao.module.trade.service.depositproductrelation.DepositProductRelationService;

@Tag(name = "管理后台 - 钢瓶与押金关系")
@RestController
@RequestMapping("/trade/deposit-product-relation")
@Validated
public class DepositProductRelationController {

    @Resource
    private DepositProductRelationService depositProductRelationService;

    @PostMapping("/create")
    @Operation(summary = "创建钢瓶与押金关系")
    @PreAuthorize("@ss.hasPermission('trade:deposit-product-relation:create')")
    public CommonResult<Long> createDepositProductRelation(@Valid @RequestBody DepositProductRelationSaveReqVO createReqVO) {
        return success(depositProductRelationService.createDepositProductRelation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新钢瓶与押金关系")
    @PreAuthorize("@ss.hasPermission('trade:deposit-product-relation:update')")
    public CommonResult<Boolean> updateDepositProductRelation(@Valid @RequestBody DepositProductRelationSaveReqVO updateReqVO) {
        depositProductRelationService.updateDepositProductRelation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除钢瓶与押金关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:deposit-product-relation:delete')")
    public CommonResult<Boolean> deleteDepositProductRelation(@RequestParam("id") Long id) {
        depositProductRelationService.deleteDepositProductRelation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得钢瓶与押金关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:deposit-product-relation:query')")
    public CommonResult<DepositProductRelationRespVO> getDepositProductRelation(@RequestParam("id") Long id) {
        DepositProductRelationDO depositProductRelation = depositProductRelationService.getDepositProductRelation(id);
        return success(BeanUtils.toBean(depositProductRelation, DepositProductRelationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得钢瓶与押金关系分页")
    @PreAuthorize("@ss.hasPermission('trade:deposit-product-relation:query')")
    public CommonResult<PageResult<DepositProductRelationRespVO>> getDepositProductRelationPage(@Valid DepositProductRelationPageReqVO pageReqVO) {
        PageResult<DepositProductRelationDO> pageResult = depositProductRelationService.getDepositProductRelationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DepositProductRelationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出钢瓶与押金关系 Excel")
    @PreAuthorize("@ss.hasPermission('trade:deposit-product-relation:export')")
    @OperateLog(type = EXPORT)
    public void exportDepositProductRelationExcel(@Valid DepositProductRelationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DepositProductRelationDO> list = depositProductRelationService.getDepositProductRelationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "钢瓶与押金关系.xls", "数据", DepositProductRelationRespVO.class,
                        BeanUtils.toBean(list, DepositProductRelationRespVO.class));
    }

}